<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1,user-scalable=no">
    <title>登录 - 答题系统</title>
    
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f0f2f5;
        }

        .container {
            max-width: 400px;
            margin: 100px auto;
            padding: 40px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .form-group {
            margin-bottom: 20px;
        }

        input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        button {
            background: #1890ff;
            color: white;
            padding: 12px;
            width: 100%;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .error {
            color: #ff4d4f;
            margin-top: 10px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h2>用户登录</h2>
        <form id="loginForm">
            <div class="form-group">
                <input type="text" name="username" placeholder="用户名" required>
            </div>
            <div class="form-group">
                <input type="password" name="password" placeholder="密码" required>
            </div>
            <button type="submit">登录</button>
            <div style="text-align: center; margin: 20px 0; position: relative;">
                <div style="height: 1px; background: #e8e8e8; position: absolute; top: 50%; left: 0; right: 0;"></div>
                <span
                    style="background: white; position: relative; padding: 0 15px; color: #8c8c8c; font-size: 14px;">或通过以下方式登录</span>
            </div>
            <button type="button" id="ssoBtn">使用单点登录（SSO）登录</button>
        </form>
        <div id="errorMsg" class="error"></div>
        <p style="display: flex; justify-content: space-between; margin-top: 10px;">
            <a href="https://account.example.com/recover?back=https://quiz.example.com" style="color: #1890ff; text-decoration: none;">忘记密码</a>
            <a href="https://account.example.com/register?back=https://quiz.example.com"
                style="color: #1890ff; text-decoration: none;">没有账号？点击注册</a>
        </p>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();

            const formData = {
                username: e.target.username.value,
                password: e.target.password.value
            };

            try {
                const response = await fetch('/login', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(formData)
                });

                const result = await response.json();

                if (response.ok) {
                    console.log('登录成功');
                    const params = new URLSearchParams(location.search);
                    if (params.get('back')) window.location.href = params.get('back');
                    else if (result.admin) window.location.href = '/admin/list';
                    else {
                        if (window.innerWidth <= 768) {
                            window.location.href = '/list_mobile';
                        } else {
                            window.location.href = '/list';
                        }
                    }
                } else {
                    document.getElementById('errorMsg').textContent = result.error || '登录失败';
                }
            } catch (error) {
                document.getElementById('errorMsg').textContent = '网络连接异常';
            }
        });
        document.getElementById('ssoBtn').addEventListener('click', () => {
            const domain = location.host;
            const params = new URLSearchParams(location.search);
            let back = '/';
            if (params.get('back')) back = params.get('back');
            location.replace(`https://account.example.com/sso/login?system=${domain}&back=${back}`)
            // location.replace(`http://${location.hostname}:3721/sso/login?system=${domain}&back=${back}`)
        })
    </script>
</body>

</html>